package cn.com.oceansoft.osc.query.service.impl;

import cn.com.oceansoft.osc.query.domain.CaseProgress;
import cn.com.oceansoft.osc.query.domain.CaseResult;
import cn.com.oceansoft.osc.query.domain.Citizen;
import cn.com.oceansoft.osc.query.domain.InsertInfo;
import cn.com.oceansoft.osc.query.persistence.oracle.QueryMapper;
import cn.com.oceansoft.osc.query.service.IQueryService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


public class QueryServiceImpl implements IQueryService {
   // private static final Logger logger = LoggerFactory.getLogger(cn.com.oceansoft.osc.ms.service.impl.CallPoliceServiceImpl.class);
    @Resource
    QueryMapper queryMapper;


 /*   public void startService() {
        logger.info("-------【查询程序】执行开始 ---------");

        logger.info("-------【查询程序】执行开始 ---------");
    }
*/

    /**
     * 轮询
     * 10秒一次
     */

    //@Scheduled(cron = "0 0/1 * * * ? ")
    public void run() {
        System.out.println(queryMapper);
        System.out.println("==================进来了===============");
        JSONObject json  = null;
        String result;

        //查询内网同步表
        List<InsertInfo> list=queryMapper.getInsertInfo();
        if(list.size()>0){
            for(InsertInfo i:list){
                Integer type=i.getType();
                String guid=i.getGuid();
                JSONObject jsonObject= JSON.parseObject(i.getContent());
                if(type==101){
                    //重名查询
                    String xm=jsonObject.getString("xm");
                    //根据姓名查询内容
                    List<Citizen> citizens=queryMapper.getCitizenListByName(xm);
                    //创建JSON
                    result=JSON.toJSONString(citizens);
                }else if(type==102){
                    //报警结果查询
                    Integer phonh_type = json.getInteger("phone_type");
                    String phone_number = json.getString("number");
                    List<CaseResult> caseResults=queryMapper.getCaseResult(phonh_type,phone_number);
                    result=JSON.toJSONString(caseResults);
                }else if (type==103){
                    //案件进度查询
                    String sahzdbh = json.getString("sahzdbh");
                    String sjhm = json.getString("sjhm");
                    //String ds = json.getString("ds");
                    List<CaseProgress> caseProgresses=queryMapper.getCaseProgress(sahzdbh,sjhm);
                    result=JSON.toJSONString(caseProgresses);
                }else{
                    continue;
                }
                System.out.println(result);
                //插入结果集
                queryMapper.addToResultTable(guid,result);
                //更改状态
                queryMapper.setN();
            }
        }
    }
}
